package user

import (
	"enjoy_wangyashuo/internal/model/mysql"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(255);comment:用户名"`
	Password string `gorm:"type:varchar(255);comment:密码"`
	Phone    string `gorm:"type:char(11);unique;comment:用户手机号"`
	Img      string `gorm:"type:varchar(255);comment:用户头像链接"`
	Status   int8   `gorm:"type:tinyint(1);default:0;comment:'用户状态(0:正常,1:禁用)'"`
	Gender   int64  `gorm:"type:int(10);comment:'用户性别(1:男,2:女)'"`
}

// 用户登录
func (u *User) Login() (*User, error) {
	return u, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("password = ?", u.Password).Where("phone =?", u.Phone).First(&u).Error
	})
}

// 用户详情
func (u *User) UserDetail() (*User, error) {
	return u, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("phone = ?", u.Phone).
			First(&u).Error
	})
}

// 修改用户密码
func (u *User) UpdateDara() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(&u).
			Where("id = ? ", u.ID).
			Update("password", gorm.Expr("password = ?", u.Password)).Error
	})
}

// 用户注册
func (u *User) Create() (*User, error) {
	return u, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Create(&u).Error
	})
}
